package com.funmi.lizip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.funmi.lizip.entity.Image;
import com.funmi.lizip.entity.PageParam;
import com.funmi.lizip.entity.vo.ImageVo;
import com.funmi.lizip.mapper.ImageMapper;
import com.funmi.lizip.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {


    @Autowired
    private ImageMapper imageMapper;


    @Override
    public List<Map<String, Object>> RingRatioYOverYNum(Date date) {

        // 计算上一年
        //获取出来的时间加一年或一个月或一天
        Calendar calendar = Calendar.getInstance();

        // 设置起始时间
        calendar.setTime(date);
        // 减少一年
        calendar.add(Calendar.YEAR, -1);
        // calendar.add(Calendar.YEAR,0);
        Date time = calendar.getTime();
        String toYear = new SimpleDateFormat("yyyy-MM").format(time);


        // 设置起始时间 减少一个月
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        Date time2 = calendar.getTime();
        String toMonth = new SimpleDateFormat("yyyy-MM").format(time2);
        String dateNow = new SimpleDateFormat("yyyy-MM").format(date);

        List<Map<String, Object>> mapList = imageMapper.RingRatioYOverYNum(toYear, toMonth, dateNow);

        for (int i = 0; i < mapList.size(); i++) {

        }


        return mapList;


    }


    @Override
    public Map<String, Object> increaseByPercentagePoints(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -1);
        Date time = calendar.getTime();
        String toYear = new SimpleDateFormat("yyyy-MM").format(time);


        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        Date time1 = calendar.getTime();
        String toMonth = new SimpleDateFormat("yyyy-MM").format(time1);
        String dateNow = new SimpleDateFormat("yyyy-MM").format(date);

        List<LinkedHashMap<String, Long>> list = imageMapper.increaseByPercentagePoints(toYear, toMonth, dateNow);
        List<Double> list1 = new ArrayList<>();

        // 上涨的百分点
        for (Map<String, Long> map : list) {
            Set<String> strings = map.keySet();
            for (String string : strings) {
                if ("bfd".equals(string)) {
                    double num = map.get("num").doubleValue();
                    // System.out.println(num);
                    list1.add(num);
                    // System.out.println(list1);
                }
            }
        }


        // 计算上涨百分点
        // 上一年 设备增长的百分点
        double upYearSb = ((list1.get(2) - list1.get(0)) / list1.get(2)) * 100;
        // 上一年 拍摄增长的百分点
        double upYearPs = ((list1.get(3) - list1.get(1)) / list1.get(3)) * 100;
        // 上个月 设备增长的百分点
        double upMonthSb = ((list1.get(4) - list1.get(0)) / list1.get(4)) * 100;
        //double upMonthSb = ((list1.get(0) - list1.get(4)) / list1.get(0)) * 100;
        // 上个月 拍摄增长的百分点
        double upMonthPs = ((list1.get(5) - list1.get(1)) / list1.get(5)) * 100;
        //double upMonthPs = ((list1.get(1) - list1.get(5)) / list1.get(1)) * 100;
        HashMap<String, Object> map = new HashMap<>();

        map.put("upYearSb", upYearSb);
        map.put("upYearPs", upYearPs);
        map.put("upMonthSb", upMonthSb);
        map.put("upMonthPs", upMonthPs);

        return map;

    }

    @Override
    public IPage<ImageVo> pageList(PageParam<ImageVo> pageParam, String type, String imei) {
        //IPage<ImageVo> res =  imageMapper.searchPage(pageParam,type,imei);
        IPage<ImageVo> imageVoIPage = imageMapper.searchPage(pageParam, type, imei);
        return imageVoIPage;
    }

}
